--编写者：alt
--功能介绍：多倍采集
--使用方法：直接粘贴到模组环境 修改doro为人物tag 修改recipe.stacksize为自己想要的倍数

--双倍采集
local Pickable = require "components/pickable"
local oldSpawnProductLoot = Pickable.SpawnProductLoot
function Pickable:SpawnProductLoot(picker, ...)
    local loot = oldSpawnProductLoot(self, picker, ...)
    if picker and picker:HasTag("doro") then
        local pt = self.inst:GetPosition()
        if self.droppicked and self.inst.components.lootdropper ~= nil then
            pt.y = pt.y + (self.dropheight or 0)
            if self.use_lootdropper_for_product then
                self.inst.components.lootdropper:DropLoot(pt)
            else
                local num = self.numtoharvest or 1
                for i = 1, num do
                    self.inst.components.lootdropper:SpawnLootPrefab(self.product, pt)
                end
            end
        end
    end
    return loot
end

--双倍收锅
local Stewer = require "components/stewer"
local cooking = require("cooking")
local oldHarvest = Stewer.Harvest
function Stewer:Harvest(harvester, ...)
    if harvester and harvester:HasTag("doro") and self.done then
        if self.product ~= nil then
            local recipe = cooking.GetRecipe(self.inst.prefab, self.product)
            if recipe then
                local oldstacksize = recipe.stacksize
                recipe.stacksize = (recipe.stacksize or 1) * 2
                local result, b = oldHarvest(self, harvester, ...)
                recipe.stacksize = oldstacksize
                return result, b
            end
        end
    end
    return oldHarvest(self, harvester, ...)
end

--双倍钓鱼
local FishingRod = require "components/fishingrod"
local oldReel = FishingRod.Reel
function FishingRod:Reel(...)
    local a, b = oldReel(self, ...)
    local man = self.fisherman
    if man and man:HasTag("doro") and self.caughtfish and self.caughtfish.prefab then
        local new = SpawnPrefab(self.caughtfish.prefab)
        if new then
            if man.components.inventory ~= nil then
                man.components.inventory:GiveItem(new, nil, man:GetPosition())
            else
                local x, y, z = man.Transform:GetWorldPosition()
                new.components.inventoryitem:DoDropPhysics(x, y, z, true)
            end
        end
    end
    return a, b
end

--双倍掉落 inst为玩家对应的实体
local function doubleIt(inst, loot)
    local pt = inst:GetPosition()
    local stackable = loot.components.stackable
    if stackable and loot:IsValid() then
        stackable:SetStackSize(stackable:StackSize() * 2)
    else
        local new = SpawnPrefab(loot.prefab)
        local inventory = inst.components.inventory
        if inventory and new.components.inventoryitem then
            inventory:GiveItem(new, nil, pt)
        else
            new.Transform:SetPosition(pt:Get())
        end
    end
end
local function onpicksomething(inst, data)
    local loot = data.loot
    if loot then
        if loot.components then
            doubleIt(inst, loot)
        else
            for i, item in ipairs(loot) do
                doubleIt(inst, item)
            end
        end
    end
    local object = data.object
    local picker = inst
    if object and object.has_flower then
        -- You get a cactus flower, yay.
        local cactus_flower = SpawnPrefab("cactus_flower")
        cactus_flower.components.inventoryitem:InheritWorldWetnessAtTarget(object)
        if picker.components.inventory ~= nil then
            picker.components.inventory:GiveItem(cactus_flower, nil, object:GetPosition())
        else
            local x, y, z = object.Transform:GetWorldPosition()
            cactus_flower.components.inventoryitem:DoDropPhysics(x, y, z, true)
        end
    end
end
local function onkilled(player, data)
    local victim = data.victim
    local stackmult = data.stackmult or 1
    for i = 1, stackmult do
        if victim.components.lootdropper then
            victim.components.lootdropper:DropLoot(victim:GetPosition())
        end
    end
end
inst:ListenForEvent("killed", onkilled)
inst:ListenForEvent("picksomething", onpicksomething)
